/*
 * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its documentation for
 * NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html" for further important copyright
 * and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.eastmountain.chinese.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import com.eastmountain.chinese.db.exceptions.DBAccessException;
import com.eastmountain.chinese.db.flashcards.records.ChineseCharacterProperties;
import com.eastmountain.chinese.errormsgs.ErrorMsgs;
import com.eastmountain.chinese.gui.controllers.FlashcardsMenuBar;
import com.eastmountain.chinese.gui.controllers.HZWindowAdapter;
import com.eastmountain.chinese.gui.controllers.ToolBarPanel;
import com.eastmountain.chinese.observable.HZFlashcardsDataModel;
import com.eastmountain.util.logging.LoggerUtils;

@SuppressWarnings("serial" )
public class MainPanel extends JPanel implements Observer {
	
	private String dbIndicator;

    private HZFlashcardsDataModel hzFlashcardsDataModel = null;

    private Vector<ChineseCharacterProperties> chineseCharacterPropertiesGroup;

    private GridBagConstraints constraints;

    private JFrame frame;

    private FlashcardsMenuBar flashcardsMenuBar;
    private HanZiCounterPanel hanZiCounterPanel;

    private JSplitPane leftSplitPane;

    private JPanel flashcardPanel;
    private HanZiDisplayPanel hanZiDisplayPanel;
    private ToolBarPanel toolBarPanel;

    private JSplitPane rightSplitPane;

    private static int FRAME_WIDTH = 500;
    private static int FRAME_HEIGHT = 350;

    protected static final Border bevelBorder = new BevelBorder( BevelBorder.LOWERED );
    protected static final Border emptyBorder = new EmptyBorder( 6, 6, 6, 6 );
    protected static final Border compoundBorder = new CompoundBorder( MainPanel.bevelBorder, MainPanel.emptyBorder );

    private String frameTitle = "";
    
    private static String projectName = "hzflashcards";
    protected static Logger logger = LoggerUtils.setUpLogger( projectName );

    /***********************************************************************************
     *
     *  MainPanel
     *
     **********************************************************************************/

    public MainPanel( String dbIndicator ) {

        // TODO shut down DB just in case it's
        //      already running
    	
    	this.dbIndicator = dbIndicator;

        int w, h;
        
        hzFlashcardsDataModel
            = HZFlashcardsDataModel.getInstance( dbIndicator );
        hzFlashcardsDataModel.initializeFlashCards();
        
        //JFrame.setDefaultLookAndFeelDecorated( true );
        //JDialog.setDefaultLookAndFeelDecorated( true );

        //MetalLookAndFeel.setCurrentTheme( new OxideTheme() );

        //LookAndFeelAddons.setAddon(  new NimbusLookAndFeelAddons() );
/*
        try {
            UIManager.setLookAndFeel( new MetalLookAndFeel() );
        } catch ( UnsupportedLookAndFeelException ulafe ) {
            System.out.println( "Substance failed to set" );
        }

*/        // TODO Java version before creating splash screen
/*
        final SplashScreen splash = SplashScreen.getSplashScreen();

        if ( splash == null ) {

            System.out.println( "???????   SplashScreen.getSplashScreen() returned null" );

        }
*/

        flashcardsMenuBar = new FlashcardsMenuBar();

        constraints = new GridBagConstraints();

        // constraints.fill = GridBagConstraints.VERTICAL;
        constraints.insets = new Insets( 2, 2, 2, 2 );

        flashcardPanel = new JPanel( new BorderLayout() );

        flashcardPanel.setPreferredSize( new Dimension( 400, MainPanel.FRAME_HEIGHT ) );

        hanZiCounterPanel = new HanZiCounterPanel();
        hanZiCounterPanel.setPreferredSize( new Dimension( 250, 70 ) );

        hanZiDisplayPanel = new HanZiDisplayPanel();
        hanZiDisplayPanel.setPreferredSize( new Dimension( 275, 75 ) );

        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 0.75;
        constraints.weighty = 0.75;

        constraints.gridwidth = 3;
        constraints.gridx = 0;
        constraints.gridy = 0;

        flashcardPanel.add( hanZiCounterPanel, BorderLayout.PAGE_START );

        constraints.gridwidth = 3;
        constraints.gridx = 0;
        constraints.gridy = 1;

        flashcardPanel.add( hanZiDisplayPanel, BorderLayout.CENTER );

        constraints.gridwidth = 3;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 0;
        constraints.gridy = 2;

        add( flashcardPanel );
        
        frameTitle = hzFlashcardsDataModel.getFlashCardSource();

        frame = new JFrame( frameTitle );
        frame.addWindowListener( new HZWindowAdapter( frame ) );

        setOpaque( true );
        frame.setJMenuBar( flashcardsMenuBar );

        ErrorMsgs errorMsgs = new ErrorMsgs( this );

        //  add the previously created panels to the content pane.

        frame.getContentPane().add( this );

        // Display the window.

        w = getWidth();
        h = getHeight();
        System.out.println( "*-*-*-*-*-* Before pack w = " + w + "  h = " + h );

        frame.pack();

        w = getWidth();
        h = getHeight();
        System.out.println( "*-*-*-*-*-* After pack w = " + w + "  h = " + h );

        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation( dimension.width / 2 - w / 2, dimension.height / 2 - h / 2 );

        Dimension fcs = flashcardPanel.getSize();
        System.out.println( "*-*-*-*-*-* flashcardPanel size: w = " + fcs.width + "  h = "
                                        + fcs.height );
        flashcardPanel.setPreferredSize( fcs );
        flashcardPanel.setMinimumSize( fcs );
        
        frame.toFront(); 
        frame.setVisible( true );

        hzFlashcardsDataModel.setMainFrame( frame );
 
    }

    /***********************************************************************************
     *
     * update
     *
     **********************************************************************************/

    public void update( Observable o, Object arg ) {

    }
    
    public void finalize() {
        
        if ( ! ( hzFlashcardsDataModel == null ) ) {          
            
            try {
                
                hzFlashcardsDataModel.shutdown();
                
            } catch ( DBAccessException dbAccessException ) {
              
                logger.log( Level.WARNING, "Database shutdown failed. "
                            + dbAccessException.getMessage() );
                
                System.out.println( "\nWARNING: Database shutdown failed.\n" );
                
                dbAccessException.printStackTrace();
                
            }
            
        }
    }

}
